package com.yanqu.road.dao.impl.union.cross.huaben;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.huaben.server.UnionHuaBenPatronsData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionHuaBenPatronsDataDaoImpl extends TempDataObjectDao<UnionHuaBenPatronsData> {

    public static String getUnionHuaBenPatronsDataSql = "select * from `t_u_hua_ben_patrons` where `userId`=?";
    public Map<Integer, UnionHuaBenPatronsData> getUnionHuaBenPatronsData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UnionHuaBenPatronsData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUnionHuaBenPatronsDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionHuaBenPatronsData patronsData = parse(rs);
                result.put(patronsData.getPatronsId(), patronsData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertUnionHuaBenPatronsDataSql = "insert into `t_u_hua_ben_patrons`(userId,patronsId,sendTimes,resetTime,weekId) values (?,?,?,?,?)";

    @Override
    protected boolean addData(UnionHuaBenPatronsData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSendTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        if (getDb().executeNoneQuery(insertUnionHuaBenPatronsDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUnionHuaBenPatronsDataSql = "update `t_u_hua_ben_patrons` set sendTimes=?,resetTime=?,weekId=? where userId=? and patronsId=?";
    
    @Override
    protected boolean updateData(UnionHuaBenPatronsData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getSendTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        if (getDb().executeNoneQuery(updateUnionHuaBenPatronsDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UnionHuaBenPatronsData parseData(ResultSet rs) throws SQLException {
        UnionHuaBenPatronsData data = new UnionHuaBenPatronsData();
        data.setUserId(rs.getLong("userId"));
        data.setPatronsId(rs.getInt("patronsId"));
        data.setSendTimes(rs.getInt("sendTimes"));
        data.setResetTime(rs.getInt("resetTime"));
        data.setWeekId(rs.getInt("weekId"));

        return data;
    }
}
